var layer, form;
var index = {
    /**
     *@description 菜单操作接口url集合 统一管理
     *@author XieXing
     *@date 2020/8/23 15:35
     */
    URL: {
        loginUrl: "/login",
        mainUrl: function (navId, navName) {
            return "main?navId=" + navId + "&navName=" + navName;
        }
    },
    init: function () {
        layui.use(['form', 'layer'], function () {
            layer = layui.layer, form = layui.form;
            $("#loginBtn").click(function () {
                index.edit_model("登录");
            });

            $("#formSubmitBtn").click(function () {
                index.login();
            });

            //点击验证码图片获取验证码
            $("#resCode").click(function () {
                $(this).attr("src", "/getGifCode?" + Math.random());
            });

            //解决iframe跳转到登录页面出现问题
            if (window != top) {
                top.location.href = location.href;
            }

            /**
             *@description 系统模块点击事件
             *@author XieXing
             *@date  15:52
             */
            $(".card").click(function () {
                var navId = $(this).attr("v");
                if (navId) {
                    var navName = $(this).find("p:first").text();
                    window.open(index.URL.mainUrl(navId, navName));
                } else {
                    index.edit_model("登录");
                }
            })

        });
    },

    /**
     *@description 登陆
     *@author XieXing
     *@date 2020/9/24 15:20
     */
    login: function () {
        var userName = $("#userPhone").val();//用户名用手机号代替
        var userPassword = $("#userPassword").val();//密码
        var code = $("#code").val();
        var verifyRes = index.login_verify(userName, userPassword, code);
        if (!verifyRes.status) {
            $("#resCode").trigger("click");
            layer.msg(verifyRes.msg, {icon: 2, time: 2000});
            return false;
        }
        var params = {userName: userName, userPassword: userPassword, code: code};
        var loading = layer.load(0);
        $.post(index.URL.loginUrl, params, function (result) {
            layer.close(loading);
            if (result.code != 200) {
                $("#resCode").trigger("click");
                layer.msg(result.data, {icon: 2})
                return false;
            }
            location.href = "/";
        })
    },
    /**
     *@description 登录前验证
     *@author XieXing
     *@date 2020/9/24 15:20
     */
    login_verify: function (userName, userPassword, code) {
        var res = {status: true, msg: "验证成功"};
        if (!userName) {
            res.status = false;
            res.msg = "手机号不能为空";
        }
        if (!userPassword) {
            res.status = false;
            res.msg = "密码不能为空";
        }
        if (!code) {
            res.status = false;
            res.msg = "验证码为空";
        }
        return res;
    },
    /**
     *@description 编辑弹出层
     *@author XieXing
     *@date 2020/8/18 22:00
     */
    edit_model: function (modelName) {
        layer.open({
            type: 1,
            area: ['600px', '300px'],
            title: modelName,
            content: $("#dataModel"),
            btn: ['登录', '取消'],
            btn1: function (index, layero) {
                $("#formSubmitBtn").trigger("click");
                return false;
            },
            btn2: function (index, layero) {
                layer.closeAll();
                return false;
            },
            cancel: function (layero, index) {
                layer.closeAll();
            },
            success: function (layero, index) {
            }
        });
    }
};
index.init();